import StorageUtils from '../../app/storage';
import DB from '../../app/db';
import Utils from "../../utils/index";

export default (callback, props)=> {
  if (__LOCAL__) {
    StorageUtils.createUser();
    callback();
  } else {
    new Promise(function(resolve, reject) {
      StorageUtils.getUserInfo(resolve, reject);
    }).then(function (r) {
      let userInfo = StorageUtils.getUser();
      DB.WechatAPI.getProprietorByOpenId({openId: userInfo.openId}).then(
        function (content) {
          if (content && content.id) {
            StorageUtils.updateUser({proprietorId: content.id});
            StorageUtils.setStorage('proprietor', content);
          } else {
            StorageUtils.updateUser({proprietorId: ''});
            StorageUtils.removeStorageItem('proprietor');
          }
        },
        function (error) {
          console.log(error);
          StorageUtils.updateUser({proprietorId: ''});
          StorageUtils.removeStorageItem('proprietor');
          //alert(JSON.stringify(userInfo.openId));
          // Control.replace('/xq/register', userInfo)
          /*WeixinJSBridge.call('closeWindow');*/
        }
      ).catch(function (reason) {
        Utils.myAlert(reason);
      });
      callback();
    }).catch(function (reason) {
      Utils.myAlert(reason);
      StorageUtils.createUser();
      callback();
    });
/*
    let userInfo = StorageUtils.getUser();
    if (userInfo && userInfo.openId) {
      if (userInfo.openId === 'xxx') {
        new Promise(function(resolve, reject) {
          StorageUtils.getUserInfo(resolve, reject);
        }).then(function (r) {
          userInfo = StorageUtils.getUser();
          DB.WechatAPI.getProprietorByOpenId({openId: userInfo.openId}).then(
            function (content) {
              if (content && content.id) {
                StorageUtils.updateUser({proprietorId: content.id});
                StorageUtils.setStorage('proprietor', content);
              }
            },
            function (error) {
              console.log(error);
              //alert(JSON.stringify(userInfo.openId));
              // Control.replace('/xq/register', userInfo)
              /!*WeixinJSBridge.call('closeWindow');*!/
            }
          ).catch(function (reason) {
            Utils.myAlert(reason);
          });
          callback();
        }).catch(function (reason) {
          Utils.myAlert(reason);
          StorageUtils.createUser();
          callback();
        });
      } else {
        callback();
      }
    } else {
      new Promise(function(resolve, reject) {
        StorageUtils.getUserInfo(resolve, reject);
      }).then(function (r) {
        userInfo = StorageUtils.getUser();
        DB.WechatAPI.getProprietorByOpenId({openId: userInfo.openId}).then(
          function (content) {
            if (content && content.id) {
              StorageUtils.updateUser({proprietorId: content.id});
              StorageUtils.setStorage('proprietor', content);
            }
          },
          function (error) {
            console.log(error);
            //alert(JSON.stringify(userInfo.openId));
            // Control.replace('/xq/register', userInfo)
            /!*WeixinJSBridge.call('closeWindow');*!/
          }
        );
        callback();
      }).catch(function (reason) {
        StorageUtils.createUser();
        callback();
      });
    }*/
  }
}
